package com.ruoyi.stuinfo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.stuinfo.domain.HomeworkStatsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.stuinfo.mapper.AssMapper;
import com.ruoyi.stuinfo.domain.Ass;
import com.ruoyi.stuinfo.service.IAssService;

/**
 * 作业情况Service业务层处理
 *
 * @author zc
 * @date 2025-05-16
 */
@Service
public class AssServiceImpl implements IAssService
{
    @Autowired
    private AssMapper assMapper;

    /**
     * 查询作业情况
     *
     * @param submissionId 作业情况主键
     * @return 作业情况
     */
    @Override
    public Ass selectAssBySubmissionId(Long submissionId)
    {
        return assMapper.selectAssBySubmissionId(submissionId);
    }

    /**
     * 查询作业情况列表
     *
     * @param ass 作业情况
     * @return 作业情况
     */
    @Override
    public List<Ass> selectAssList(Ass ass)
    {
        return assMapper.selectAssList(ass);
    }

    /**
     * 新增作业情况
     *
     * @param ass 作业情况
     * @return 结果
     */
    @Override
    public int insertAss(Ass ass)
    {
        return assMapper.insertAss(ass);
    }

    /**
     * 修改作业情况
     *
     * @param ass 作业情况
     * @return 结果
     */
    @Override
    public int updateAss(Ass ass)
    {
        return assMapper.updateAss(ass);
    }

    /**
     * 批量删除作业情况
     *
     * @param submissionIds 需要删除的作业情况主键
     * @return 结果
     */
    @Override
    public int deleteAssBySubmissionIds(Long[] submissionIds)
    {
        return assMapper.deleteAssBySubmissionIds(submissionIds);
    }

    /**
     * 删除作业情况信息
     *
     * @param submissionId 作业情况主键
     * @return 结果
     */
    @Override
    public int deleteAssBySubmissionId(Long submissionId)
    {
        return assMapper.deleteAssBySubmissionId(submissionId);
    }

    @Override
    public List<Ass> selectAssList_handteacher(String teacherId) {
        return assMapper.selectAssList_handteacher(teacherId);
    }

    @Override
    public List<Ass> selectAssList_teacher(String teacherId) {
        return assMapper.selectAssList_teacher(teacherId);
    }

    @Override
    public HomeworkStatsVO getHomeworkSubmissionStats() {
        // 查询作业状态统计（返回多条记录）
        List<Map<String, Object>> statusList = assMapper.countSubmissionsByStatus();

        // 初始化统计结果
        Map<String, Integer> statusCountMap = new HashMap<>();
        statusCountMap.put("已提交", 0);
        statusCountMap.put("已批改", 0);
        statusCountMap.put("迟交", 0);

        // 处理查询结果
        for (Map<String, Object> statusItem : statusList) {
            String statusName = (String) statusItem.get("status");
            Integer count = ((Number) statusItem.get("count")).intValue();
            statusCountMap.put(statusName, count);
        }

        // 构建返回对象
        return HomeworkStatsVO.builder()
                .submitted(statusCountMap.get("已提交"))
                .graded(statusCountMap.get("已批改"))
                .late(statusCountMap.get("迟交"))
                .build();
    }
}
